import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 九转苍翎
 * Date: 2025/8/13
 * Time: 13:19
 */
public class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>> result = new ArrayList<>();
        for(int i = 0;i < nums.length;){
            for(int j = i + 1;j < nums.length;){
                int left = j + 1;
                int right = nums.length - 1;
                while(left < right){
                    long sum = (long) nums[i] + nums[j] + nums[left] + nums[right];
                    if(sum == target){
                        result.add(new ArrayList<>(Arrays.asList(nums[i], nums[j], nums[left++], nums[right--])));
                        while(left < right && nums[left] == nums[left-1]) left++;
                        while(left < right && nums[right] == nums[right+1]) right--;
                    } else if (sum > target) {
                        right--;
                    }else  {
                        left++;
                    }
                }
                do j++;
                while (j < nums.length && nums[j] == nums[j - 1]);
            }
            do i++;
            while (i < nums.length && nums[i] == nums[i - 1]);
        }
        return result;
    }
    public static void main(String[] args) {
        Solution s = new Solution();
        List<List<Integer>> ret = s.fourSum(new int[]{1000000000,1000000000,1000000000,1000000000}, -294967296);
        for (List<Integer> list : ret) {
            for (Integer num : list) {
                System.out.print(num + " ");
            }
            System.out.println();
        }
    }
}